package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.service.manage.material.PurchaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Zephyr
 * @Description:    移动 - - 采购计划
 * @Date 2018/10/23
 * */
@Controller
@RequestMapping(value = "/mb_api/purchase")
public class MbPurchaseController extends MbBaseController {

    private final PurchaseService purchaseService;

    @Autowired
    public MbPurchaseController(PurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }

    /**
     * 审批
     * @return
     * */
    @ResponseBody
    @RequestMapping(value = "/approval", method = RequestMethod.POST)
    public ApiResult approval(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String appr = body.getString("appr");
        String apprType = body.getString("apprType");
        String code = body.getString("code");
        String fBackReason = body.getString("fBackReason");
        if(StringUtils.isEmpty(appr) || StringUtils.isEmpty(apprType) || StringUtils.isEmpty(code)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        try{
            return purchaseService.saveAppr(Integer.parseInt(appr), apprType, Long.parseLong(code), userId, fBackReason, beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }
    }


}
